# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>

_realname=exiv2
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=0.25
pkgrel=2
pkgdesc="Exif and Iptc metadata manipulation library and tools (mingw-w64)"
arch=('any')
url="http://exiv2.org"
license=("GPL")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-pkg-config"
             "${MINGW_PACKAGE_PREFIX}-libtool")
depends=("${MINGW_PACKAGE_PREFIX}-expat"
         "${MINGW_PACKAGE_PREFIX}-gettext"
         "${MINGW_PACKAGE_PREFIX}-zlib")
options=('strip' 'staticlibs')
source=("http://www.exiv2.org/${_realname}-${pkgver}.tar.gz"
        0001-fix-cr-for-awk-in-configure.mingw.patch)
sha256sums=('c80bfc778a15fdb06f71265db2c3d49d8493c382e516cb99b8c9f9cbde36efa4'
            '10b2f78aede133291dd77cb1d3e8deea552420fcba5767d10186d2d3b96b7614')

prepare() {
  cd ${srcdir}/${_realname}-${pkgver}
  patch -p1 -i ${srcdir}/0001-fix-cr-for-awk-in-configure.mingw.patch
}

build() {
  export lt_cv_deplibs_check_method='pass_all'
  CPPFLAGS+=" -D_FILE_OFFSET_BITS=64"

  [[ -d ${srcdir}/build-${MINGW_CHOST} ]] && rm -rf ${srcdir}/build-${MINGW_CHOST}
  cp -rf ${srcdir}/${_realname}-${pkgver} ${srcdir}/build-${MINGW_CHOST}
  cd ${srcdir}/build-${MINGW_CHOST}

  ./configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --enable-shared \
    --enable-static

  # Fix executables extensions
  sed -i 's|BINARY = $(BINSRC:.cpp=)|BINARY = $(BINSRC:.cpp=$(EXEEXT))|;
   s|EXIV2BIN = $(EXIV2MAIN:.cpp=)|EXIV2BIN = $(EXIV2MAIN:.cpp=$(EXEEXT))|;
   s|MCBIN = $(MCMAIN:.cpp=)|MCBIN = $(MCMAIN:.cpp=$(EXEEXT))|;
   s|-o $@ $(LIBRARY) $@.o|-o $@ $(LIBRARY) $(BINOBJ)|
   s|$(BINARY): %: %.o lib|$(BINARY): lib $(BINOBJ)|' src/Makefile

  make
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  make DESTDIR="${pkgdir}" install
}
